package com.gopay.checkorder.service.task;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.util.DateUtils;
import com.gopay.common.util.FtpUtil;
import com.gopay.common.util.SendMail;

/**
 * 中信银行借记卡快捷支付对账文件下载
 * 
 * @ClassName: CiticDExpayDownloadOrderTaskImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author w_jiqing
 * @date 2016年10月11日 上午9:51:20
 */
public class CiticDExpayDownloadOrderTaskImpl implements Task {

    private final static Logger logger = LoggerFactory.getLogger("CHECK_ORDER");

    @Resource(name = "citicdexpayConfig")
    private Map<String, String> citicdexpayConfig;

    private int port = 21;
    private String fileHeader = "GFBZF";

    @Override
    public void doTask() {
        logger.error("CiticDExpayDownloadOrderTaskImpl: 定时任务start");
        try {
            if ("off".equals(citicdexpayConfig.get("mailSwitch"))) {
                logger.error("CiticDExpayDownloadOrderTaskImpl: 不进行ftp操作直接返回!");
                return;
            }

            String url = citicdexpayConfig.get("ftp.url");
            String username = citicdexpayConfig.get("ftp.username");
            String password = citicdexpayConfig.get("ftp.password");
            String locpath = citicdexpayConfig.get("ftp.locpath");
            String FTime = DateUtils.getYesterday();
            String FPath = "/citic/expay_upload";
            String qsdzName = fileHeader + "_QSDZ_" + FTime + "_1.txt"; // 清算对账文件
            String encoding = "UTF-8";
            List sendFile = new ArrayList();
            List names = new ArrayList();
            String messageText = "";
            FtpUtil ftpUtil = new FtpUtil();


            // 判断本地服务器是否存在清算对账文件
            if (!(new File(locpath + qsdzName).exists())) {
                // 判断清算对账文件是否存在
                if (ftpUtil.exist(url, port, username, password, FPath, qsdzName, encoding)) {
                    logger.error("CiticDExpayDownloadOrderTaskImpl " + qsdzName + "exist!");
                    // 下载签约对账文件
                    if (ftpUtil.downFile(url, port, username, password, FPath, qsdzName, locpath, qsdzName, encoding)) {
                        logger.error("CiticDExpayDownloadOrderTaskImpl " + qsdzName + "downLoad success !");
                        names.add(qsdzName);
                    }
                }
            } else {
                logger.error("CiticDExpayDownloadOrderTaskImpl local " + qsdzName + "清算对账文件已经成功下载 , 不在进行ftp操作 ！");
            }

            if (names.size() < 1) {
                logger.error("CiticDExpayDownloadOrderTaskImpl : 中信银行借记卡快捷支付对账文件为null或本地已成功下载！");
                return;
            } else {
                messageText = FTime + ":中信银行借记卡快捷支付文件见附件！" + qsdzName + "为: 清算对账文件 。 ";

                for (int i = 0; i < names.size(); i++) {
                    sendFile.add(locpath + (String) names.get(i));
                }
            }
            String subject = "中信银行借记卡快捷支付对账文件";
            String[] cc = citicdexpayConfig.get("sendmail.cc").split(","); // 获取多个抄送人

            List ccList = Arrays.asList(cc);

            SendMail.sendMessage(citicdexpayConfig.get("sendmail.host"), citicdexpayConfig.get("sendmail.sender"),
                    citicdexpayConfig.get("sendmail.username"), citicdexpayConfig.get("sendmail.password"),
                    citicdexpayConfig.get("sendmail.receiver"), subject, messageText, "text/html;charset=gb2312",
                    ccList, sendFile, names);
            // String[] cc = {"835849169@qq.com","240195129@qq.com","532805398@qq.com"};
            // SendMail.sendMessage(citicdexpayConfig.get("sendmail.host"),
            // citicdexpayConfig.get("sendmail.sender"),
            // citicdexpayConfig.get("sendmail.username"),
            // citicdexpayConfig.get("sendmail.password"),
            // "w_jiqing@126.com", subject, messageText,
            // "text/html;charset=gb2312", ccList,
            // sendFile, names);

            logger.error("CiticDExpayDownloadOrderTaskImpl: 定时任务 end");

        } catch (Exception e) {
            logger.error("CiticDExpayDownloadOrderTaskImpl error: ", e);
        }

    }

}
